package com.gary;

/**
 * @author peng_rui
 * @date 2023/1/30
 */
public class SingleLazySafe {
    public static void main(String[] args) {
        Single instance1 = Single.getInstance();
        Single instance2 = Single.getInstance();

        System.out.println(instance1 == instance2);
        System.out.println("instance1 : " + instance1 );
        System.out.println("instance2 : " + instance2 );
    }
}

class Single{
    private static volatile Single instance;

    private Single() {

    }

    public static Single getInstance() {
        if (instance == null) {
            synchronized (Single.class) {
                if (instance == null) {
                    instance = new Single();
                }
            }
        }
        return instance;
    }
}
